package com.zjq.designpatterns.creation.prototype.code.destorysingleton;/*
 * 版权信息: 北京千⾥马网信科技有限公司</br>
 * Copyright ©2008-2018. All rights reserved. 京ICP备16007318号
 */

import java.io.Serializable;

/**
 * @author jessie
 * @version 1.0
 * @date 2021/1/22
 */
public class HungrySingleton implements Serializable, Cloneable {
    private static HungrySingleton hungrySingleton = null;
    static{
        hungrySingleton = new HungrySingleton();
    }

    private HungrySingleton(){
        if(hungrySingleton!=null){
            throw new RuntimeException("单例禁止反射调用");
        }
    }

    public static HungrySingleton getInstance(){
        return hungrySingleton;
    }

    private Object readResolve(){
        return hungrySingleton;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //这里会变成2个对象
//        return super.clone();
        return getInstance();
    }


}
